Appendix A: PostScript Overview

XCoding PostScript by hand gives you the most control possible over the Xshape and appearance of the figure; for example, few conventional document Xpreparation packages could produce the graphic in figure XPostScript is a stack-oriented language, very similar to Forth and RPN in the way Xthat arguments are handled, yet it features strong typing and higher Xlevel control structures. It has an advanced imaging primatives, based on a Xstencil and paint imaging model. Objects are rendered on the Xpage by applying paint, which can be any color or sampled image, Xthrough a stencil, a closed geometric path that limits where the Xpaint should go. For example, a line can be described as applying black paint Xthrough a long thin stencil. This generality is preserved throughout PostScript, Xincluding in its treatment of fonts, but that does not prevent PostScript Ximplementations from taking advantage of special cases for efficiency Xconsiderations. Rarely would an actual PostScript interpreter draw a line Xby creating a thin rectangle and performing a fill operation; Xwhat is important is that its Xbehavior can be perfectly characterised as if it had. X

XPostScript has a full complement of data types, operators, and control structures. XIn general, arguments are pushed on an operand stack, then popped off and acted on by an operator Xor procedure. When PostScript encounters an object it can't execute immediately X(such as a number or a string), Xit is pushed onto the operand stack. Thus, to provide parameters to an Xoperator, simply list them before the operator name. Thus PostScript (like Xforth), is a prefix language. All expressions are thus unambiguous, and Xthe syntax is very efficient to interpret. X

XFor instance, arithmetic is easy: X

X2 2 add X
Xwould leave a `4' on the operand stack. Similarly, to add X ${\frac{{1+2}}{{2+3}}}$ + 13*9, you would type X
X1 2 add 2 3 add div 13 9 mul add X
X

XIn addition to numbers and operators there are several different types Xof objects that PostScript supports. A string, for instance, is denoted by Xtext enclosed in parenthesis. An array of objects is denoted by Xbrackets, and a procedure by braces. XAn identifier that is not a number, string, or composite object Xis a name object. There are two types of name objects, Xexecutable, meaning the value or procedure associated with the object will Xbe evaluated immediately, and literal, which is not evaluated. XA literal name has a `/` prepended to it, Xand is pushed directly on the operand stack, while an executable name is Xlooked up on the dictionary stack, and its associated value placed Xon the operand stack. XA dictionary is a data Xstructure that associates key - value pairs. XAll operators, variables, and procedures are referenced through the Xdictionary stack, which essentially establishes the hierarchical naming Xenvironment in PostScript. X

XThere are always at least two entries on the dictionary stack: the system dictionary and Xthe user dictionary. The system dictionary contains all the bindings for the Xbuilt-in operators, while the user dictionary holds user variables and Xprocedures. A PostScript program is free to modify the user dictionary (and to Xadd new entries on the stack), but can not write to the system dictionary. X

XAssigment is with the def operator, which takes a literal name and value Xand stores it in the topmost dictionary; to initialize π you Xcould say X

X/pi 3.14 def X
XAfter this def, typing the executable name pi Xwill cause PostScript to look up that name in the dictionary stack and place 3.14 on the operand stack. X3XFor instance, X
Xpi r 2 exp mul X
Xwould evaluate πr2 (of course, if r had not been Xdefined, an error would have been generated). X

X

\begin{figure}X\par
X\begin{picture}(340,220)
X\put(0,120){\tt newpath }
X\put(0...
...icture}X\par
X\centerline{Figure 5: a sample PostScript\ program.}
X\end{figure}
XThe PostScript language has a rich collection of operators, including Xthe usual stack operators (pop, dup, exch, ...), arithmetic operators (add, mul, Xsin, ...), and control operators (if, loop, for ...), as well as Xmore advanced operators Xfor manipulating dictionaries, arrays, strings, and files. X

XWhile PostScript has all the capabilities of a general purpose language, it Xis first and foremost a page description language, Xand as such has a whole range of operators that manipulate Xthe graphics state and place marks on the page. XAll values given to graphic primitives in PostScript are transformed through the Xcurrent transformation matrix (CTM) before any marks are made on the Xpage. Thus the CTM establishes the scale and orientation of the coordinate Xsystem in which a program will run. XThe default PostScript CTM produces a coordinate system in PostScript points (PostScript points Xare the same as `big points' (bp) in TEX- 72 to the inch) Xwith the origin in the lower left hand corner of the page. XThe CTM can be scaled, rotated, or translated dynamically Xby a PostScript program. X

XAn important object in the graphics environment is the current Xpath, an internal PostScript data structure that keeps Xtrack of geometric shapes. The current path is composed of an arbitrary number Xof connected or disjoint line segments, curves, and Bezier cubics. X

XThe newpath operator clears the current path, and Xthe moveto operator will move the current point to an arbitrary location. XMoveto takes two arguments, an x and y location on the page, Xso to move to (307,397) you would say 307 397 moveto. XExactly where (307,397) is on the page depends on the CTM; with the Xdefault matrix, it is roughly at the center of the page. X

XFrom the current point, an x y lineto will add a line Xsegment to (x,y) on the current path, or Xa closepath will add a segment back to the first Xpoint in the path. XTo create arcs and circles, a x y r ang1 ang2 arc will Xadd a counterclockwise segment from ang1 to ang2 of a circle of Xradius r whose center is at (x,y). XNote that none of these commands actually Xmark the page; they just build up the path structure. XTwo operators which will mark the page according to the Xcurrent path are: Xstroke, which will draw a line along the current path, and Xfill, which will paint the region enclosed by the current path. XFigure one depicts a sample PostScript program and its result. X

XText can be equally as simple; first, you must set up the current font. X

X/Times-Roman findfont 10 scalefont setfont X
Xwould set the current font to be ten point roman. XA string can be printed at the current point using the show operator. XIn PostScript  strings are delimited by parenthesis. X

XWhen a PostScript program has completed putting all useful marks onto a page, Xit should execute the Xshowpage operator, which causes the printer to print and eject the current page. X

XThus, to label the vertices in our triangle, we could modify our Xprogram as follows X

Xnewpath
X50 50 moveto
X150 50 lineto
X100 150 lineto
Xclosepath stroke
X/Times-Roman findfont 10 scalefont setfont
X45 55 moveto (a) show
X155 55 moveto (b) show
X95 55 moveto (c) show
X60 35 moveto
X(The Triangle ABC) show
Xshowpage X
Xwhich would produce: X

Xfigs/mac.pro X

figure=figs/trianglev.ps
X

XThis overview was only meant to give a flavor of the PostScript Xlanguage, and therefore has only touched on the simplest Xof it's commands. For a more thorough introduction, Xconsult the PostScript Language Tutorial and Cookbook Xand PostScript Language Reference Manual from Adobe Systems. X

X